//index.js
//获取应用实例
const app = getApp()
let Axios = require('../../common/js/ajax.js'); //引入Axios方法
let WxValidate = require('../../common/js/WxValidate.js'); //表单验证
let Mtils = require('../../common/js/Mtils.min.js');
Mtils = Mtils.Mtils
Page({
	data: {
		openid: '',
		nickName: '',
		avatarUrl: '',
		curentNav: 2, //当前导航
		address: {},
		pickerValueDefault: '',
		titles: '',
		isIpx:app.globalData.isIpx?true:false
	},
	//事件处理函数
	onLoad: function(e) {
		var _this = this
		this.initValidate()
		this.uid = app.globalData.uid || ''
		this.Md5 = Mtils.security.hex_md5(this.uid) + app.globalData.secret;
		this.Md5 = Mtils.security.hex_md5(this.Md5);
		this.timesp = Mtils.now();
		if (e.hasOwnProperty('id')) {
			Axios.get(app.globalData.baseUrl + 'App/User/UserInfo/address_detail', {
				uid: _this.uid,
				time: _this.timesp,
				sign: _this.Md5,
				address_id: e.id
			}, (res) => {
				console.log(res)
				if (res.status == 1) {
					var data = res.info
					_this.setData({
						address: data
					})
					_this.setData({
						pickerValueDefault: data.path
					})
					_this.picker = this.selectComponent("#city");
					_this.picker.int_address();
					_this.picker.init();
				}
			})
		} else {
			_this.picker = this.selectComponent("#city");
			_this.picker.int_address();
			_this.picker.init();
		}
	},
	onReady: function() {
		wx.setNavigationBarTitle({
			title: '添加地址'
		})

	},
	switch1Change(e) {
		console.log('switch1 发生 change 事件，携带值为', e.detail.value)
	},
	submit(e) {
		var params = e.detail.value
		if (!this.WxValidate.checkForm(params)) {
			const error = this.WxValidate.errorList[0]
			this.showModal(error)
			return false
		}
		this.submitInfo(params);
	},
	submitInfo: function(e) {
		var CONST = {
			uid: this.uid,
			time: this.timesp,
			sign: this.Md5
		}
		var obj = Object.assign(e, CONST);
		Axios.post(app.globalData.baseUrl + 'App/User/UserInfo/edit_address',
			e, (res) => {
				console.log(res)
				if (res.status == 1) {
					var data = res.info
					wx.navigateBack()
				}
			})
	},
	showModal: function(error) {
		wx.showModal({
			content: error.msg,
			showCancel: false,
		})
	},
	showPicker: function() {
		this.picker.showPicker()
	},
	_getLabel: function(e) {
		this.setData({
			pickerValueDefault: e.detail.path,
			titles: e.detail.name
		})
	},
	initValidate: function() {
		/** 4-2(配置规则)*/
		const rules = {
			full_name: {
				required: true,
				rangelength: [2, 10]
			},
			mobile: {
				required: true,
				tel: true,
			},
			path: {
				required: true,
			},
			// 配置false可关闭验证
			address: {
				required: true,
			},

		}
		// 验证字段的提示信息，若不传则调用默认的信息
		const messages = {
			full_name: {
				required: '请输入姓名',
				rangelength: '请输入2~10个汉字个汉字'
			},
			mobile: {
				required: '请输入11位手机号码',
				tel: '请输入正确的手机号码',
			},
			path: {
				required: '请选择地址',
			},
			address: {
				required: '请输入详细地址',
			}
		}
		// 创建实例对象
		this.WxValidate = new WxValidate.WxValidate(rules, messages)
		/**
		 * 也可以自定义验证规则
		 */
		// 		this.WxValidate.addMethod('assistance', (value, param) => {
		// 			return this.WxValidate.optional(value) || (value.length >= 1 && value.length <= 2)
		// 		}, '请勾选 《顺风男服务协议》')
	}
})
